import type { RequestConfig } from 'umi';
import { history } from 'umi';
import errorHandler from './utils/errorHandle';
// 获取baseUrl，
let baseUrl: string = '';
if (process.env.NODE_ENV === 'development') {
  baseUrl = '/api';
} else {
  baseUrl = 'https://messln.cn/api/';
}
const requestInterceptor: any = (url: string, options: any) => {
  const headers = {
    'Content-Type': 'application/json;charset=UTF-8',
    Accept: 'application/json',
  };
  return {
    url: `${baseUrl}${url}`,
    options: { ...options, headers, method: options.method ?? 'GET' },
  };
};
const responseInterceptor: any = (response: any) => {
  // console.log('什么玩意啊', response);
  if (
    response.status === 401 ||
    response.status === 403 ||
    response.status === '401'
  ) {
    history.push('/home');
  }
  return response;
};
export const request: RequestConfig = {
  errorConfig: {
    adaptor: (res: any) => {
      return {
        success: res.code === 0,
        data: res.data,
        errorCode: res.code,
        errorMessage: res.msg,
      };
    },
  },
  errorHandler,
  middlewares: [],
  requestInterceptors: [requestInterceptor],
  responseInterceptors: [responseInterceptor],
};
